﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Shapes;
using SomethingSpacialClassLib.Web;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Printing;
using System.Text;


namespace SomethingSpacial.Views.MixItUp
{
    public partial class PrintRafflePage : ChildWindow
    {

        public PrintRafflePage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(PrintRafflePage_Loaded);
            

        }

        void PrintRafflePage_Loaded(object sender, RoutedEventArgs e)
        {

            btnPrint.Click += new RoutedEventHandler(btnPrint_Click);

            var ctx = new SomethingSpacialContext();
            EntityQuery<Event> latestEventQuery = ctx.GetEventLatestQuery();
            var loadOperation = ctx.Load(latestEventQuery);

            if (!loadOperation.HasError)
                loadOperation.Completed += (s, ev) =>
                {
                    Event evt = loadOperation.Entities.SingleOrDefault();

                    raffleTicketsCanvas.DataContext = evt;
                    ticketOwnerTextBlock.Text = string.Format("{0} is entitled to 6 raffle tickets.", 
                        AppServices.WebContext.Current.User.FriendlyName);
                };


        }

        void btnPrint_Click(object sender, RoutedEventArgs e)
        {

            preparePrintData();

            printGridCanvas.Visibility = Visibility.Visible;

            PrintDocument tickets2Print = new PrintDocument();


            tickets2Print.EndPrint += (owner, args) =>
            {
                printGridCanvas.Visibility = Visibility.Collapsed;
            };

            tickets2Print.PrintPage += (owner, args) => { args.PageVisual = printGrid; };
            tickets2Print.Print("AttendeeRaffleTickets");
        }


        void preparePrintData()
        {
            textBlockAttendeeNamePrint.Text = ticketOwnerTextBlock.Text;
        }


    }
}

